<!--登陆-->
<template>
  <div class="home">
    <Header back="Home" class="heanderNav">
      <a @click="goToRegister">注册</a>
    </Header>
    <group class="from_box">
      <x-input title="账号" label-width="80px" v-model="loginForm.userName" placeholder="请输入账号"></x-input>
      <x-input title="密码" type="password" label-width="80px" v-model="loginForm.userPwd" placeholder="请输入密码"></x-input>
    </group>
    <div class="login_box">
      <x-button type="warn" text="登陆" class="login" @click.native="login"></x-button>
    </div>
    <flexbox >
      <flexbox-item>
        <div @click="forget_pwd" class="forget_pwd">忘记密码?</div>
      </flexbox-item>
    </flexbox>
    <Footer></Footer>
  </div>
</template>

<script>
  import Header from '@/components/commons/header/header'
  import Footer from '@/components/commons/footer/footer'
  import userController from '@/api/userController/index'
  import USER from '@/utils/user'
  export default {
    name: 'login',
    data () {
      return {
        loginForm:{
          userName:null,
          userPwd:null
        }
      }
    },
    components: {Header,Footer},
    created(){
      this.getLoginInfo();
      if(this.$route.params){//传有用户信息，初始化用户信息
        this.loginForm = {
          userName:this.$route.params.userName,
          userPwd:this.$route.params.userPwd
        }
      }
    },
    methods:{
      getLoginInfo(){ //获取登录信息,如果登录，重定向至上一页面
        var loginInfo = USER.getUserInfo();
        if(loginInfo.isLogin==0){ //0已登录 1未登录
          this.$router.push({name:'usercenter'});
        }
      },
      goToRegister(){
        this.$router.push({name:'register'})
      },
      forget_pwd(){
        this.$router.push({name:'findPassWordCode'});
      },
      login(){
        let formData = this.loginForm
        userController.login(formData).then(response=>{
            if(response.success){ //登录成功
              this.$vux.toast.show({
                text: response.msg,
                type: 'text',
                position: 'top'
              })              
              this.$router.push({name:'usercenter'});
            }else{//登录失败
              this.$vux.toast.show({
                text: response.msg,
                type: 'warn'
              })
            }
        }).catch(()=>{
            this.$vux.toast.show({
              text: '网络繁忙，请稍后再试',
              type: 'warn'
            })
        })
      },
      handtest(){
        console.log(this.test)
      }
    }
  }
</script>


<style scoped>
 .headerNav a{
    display: inline-block;
    width: 73.5px;
    height: 52px;
    line-height: 52px;
    text-align: right;
    padding-right: 20px;
  }
  .from_box{
    padding:25px 0;
    background: #fff;
  }
  .forget_pwd{
    font-size:12px;
    padding:10px 5px;
    color:#616161;
    text-align: right;
  }
  .login_box{
    padding:0 5px;
  }
  .login{
    margin-top:15px;
    background: #f60;
  }
</style>
